package com.jdrx.sw.sewage.service;

import com.jdrx.platform.commons.rest.beans.vo.ResponseVO;
import com.jdrx.platform.commons.rest.exception.BizException;
import com.jdrx.platform.commons.rest.factory.ResponseFactory2;
import com.jdrx.sw.sewage.beans.dto.AlarmTypeSetDTO;
import com.jdrx.sw.sewage.beans.entity.AlarmTypeSetPO;
import com.jdrx.sw.sewage.beans.vo.AlarmTypeSetVO;
import com.jdrx.sw.sewage.common.util.BeanUtils;
import com.jdrx.sw.sewage.dao.AlarmTypeSetDAO;
import com.jdrx.sw.sewage.service.base.BaseServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AlarmTypeSetServiceImpl extends BaseServiceImpl {

    public static final Logger logger = LoggerFactory.getLogger(AlarmTypeSetServiceImpl.class);

    @Autowired
    private AlarmTypeSetDAO alarmTypeSetDAO;

    public List<AlarmTypeSetVO> selectList() {
        return alarmTypeSetDAO.selectAll();
    }

    @Transactional(rollbackFor = Exception.class)
    public ResponseVO updateAlarmTypeSetSwitchById(AlarmTypeSetDTO alarmTypeSetDTO) throws BizException {
        if (null == alarmTypeSetDTO.getId()) return ResponseFactory2.ok("告警类型设置id不能为空");

        AlarmTypeSetPO alarmTypeSetPO = alarmTypeSetDAO.selectByPrimaryKey(alarmTypeSetDTO.getId());
        if (null == alarmTypeSetPO) return ResponseFactory2.ok("该设备id不存在");

        BeanUtils.copyProperties(alarmTypeSetDTO, alarmTypeSetPO);
        try {
            alarmTypeSetDAO.updateByPrimaryKey(alarmTypeSetPO);
        } catch (Exception e) {
            logger.error("更新告警类型设置失败", e);
            throw new BizException("更新告警类型设置失败");
        }
        return ResponseFactory2.ok();
    }
}
